<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/9/30
  Time: 8:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <%--<title>使用详情</title>--%>
    <link rel="stylesheet" href="/resource/css/bootstrap.css">
    <link rel="stylesheet" href="/resource/css/mine.css">
    <script type="text/javascript" src="/resource/js/DatePicker2/WdatePicker.js"></script>
    <script src="/resource/js/jquery-1.9.1.js"></script>
    <script src="/resource/js/bootstrap.js"></script>
    <script src="/js/common/layer/layer.js"></script>
    <style type="text/css">
        .alert-form-cnt{
            width: 200px;
        }
        .alert-form-title{
            margin-left:20px;
        }
        .alert-btn{
            margin-left: 43%;
            margin-top: 30px;
        }
        .alert-form-cnt>select {
            width: 180px;
        }
        .row{
            margin:0;
        }
        .form-control{
            height: 32px;
        }
    </style>
</head>
<body>
<%--<div class="col-md-12 column">--%>
<%--<div class="top-tittle">添加会员</div>--%>
<%--<div class="top-tittle-line clearfix"></div>--%>

<form action="" method="post" id="jsForm">
    <div class="row clearfix">
        <input type="hidden" id="AV" value="${storeId}">
        <span class="alert-form-title">会员姓名：</span>
        <span class="alert-form-cnt">
				<input type="text" name="memberName" id="memberName" maxlength="6"  class="form-control" >
			</span>
        <span class="alert-form-title">会员等级：</span>
        <span class="alert-form-cnt">
              <select name="memberGrade">
                  <option value="0">大众会员</option>
                  <option value="1">钻石会员</option>
                  <option value="2">皇冠会员</option>
                  <option value="3">金冠会员</option>
              </select>
			</span>
    </div>
    <div class="row clearfix">
        <span class="alert-form-title">手机号码：</span>
        <span class="alert-form-cnt">
				<input type="text" name="memberPhone" id="memeberPhone" maxlength="11" placeholder="请输入11位手机号" class="form-control" >
			</span>
        <span class="alert-form-title">会员类型：</span>
        <span class="alert-form-cnt">
				<%--<input type="text" name="memberType" class="form-control" >--%>
                <select name="memberType"  id="memberType" class="form-control">
                    <%--<option value="">请选择类型</option>--%>
                    <option value="1">普通会员</option>
                    <option value="0">众筹会员</option>
                </select>
			</span>
    </div>
    <div class="row clearfix">
        <span class="alert-form-title">所属门店：</span>
        <span class="alert-form-cnt">
                        <select id="shopId" name="shopId" onchange="checkEmpName()">
                            <option value="" >--请选择--</option>
                            <c:if test="${!empty shopList}">
                                <c:forEach items="${shopList}" var="shop">
                                    <option   value="${shop.shopID}" <c:if test="${shop.shopID eq storeId}">selected </c:if> >${shop.shopName}</option>
                                </c:forEach>
                            </c:if>
                        </select>
            </span>
        <span class="alert-form-title">预约顾问：</span>
        <span class="alert-form-cnt">
                        <select name="employeeId" id="employeeId">
                        </select>
			</span>
    </div>
    <div class="row clearfix">
        <span class="alert-form-title">会员生日：</span>
        <span class="alert-form-cnt">
				<input type="text" name="birthday2" id="birthday2" onclick="new WdatePicker()" placeholder="请输入农历生日" class="form-control form-date" >
			</span>
        <span class="alert-form-title">会员性别：</span>
        <span class="alert-form-cnt">
                <select id="sex" name="sex" required>
                  <option value="男">男</option>
                  <option selected="selected" value="女">女</option>
                </select>
			</span>
    </div>
    <div class="row clearfix">
        <span class="alert-form-title">余&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;额：</span>
        <span class="alert-form-cnt">
				<input type="text" name="money" id="money" maxlength="7" value="0" placeholder="开户余额" class="form-control" >
			</span>
        <%--   <span class="alert-form-title">积&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;分：</span>
           <span class="alert-form-cnt">
               <input type="text" name="" class="form-control" >
           </span>--%>
        <span class="alert-form-title">积&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;分：</span>
        <span class="alert-form-cnt">
				<input type="text" name="integral" id="integral" maxlength="8" value="0" placeholder="请输入整数" class="form-control" >
			</span>
    </div>
    <div class="row clearfix">
        <span class="alert-form-title">介绍人手机：</span>
        <span class="alert-form-cnt">
				<input type="text" name="introducer" id="introducer" maxlength="11"  placeholder="请输入11位手机号" class="form-control" >
			</span>
        <span class="alert-form-title">会员编号：</span>
        <span class="alert-form-cnt">
				<input type="text" name="memberNumber" id="memberNumber" class="form-control" >
			</span>
    </div>
    <input type="hidden" id="isExist">
    <button type="button" id="btn" class="btn alert-btn">添 &nbsp;&nbsp;&nbsp;&nbsp;加</button>
</form>
<%--</div>--%>
</body>
<script type="text/javascript">
    var toSend=false;
    $().ready(function(){
        window.onload=function () {
            var shopId = $("#shopId").val();
            if(shopId==''){
                checkEmpName();
            }else{
                checkEmpName();
            }
        }
        $("#btn").click(function(){
            if(valid()) {
                if (toSend) {
                    layer.msg("表单正在提交中，无需重复操作！");
                    return false;
                }
                var objButton=$(this);

                var re2 = /0?(13|14|15|18|17|19)[0-9]{9}/;
                var memberPhone = $("#memeberPhone").val();
                if(!re2.test(memberPhone)){
                    layer.msg("会员手机格式不正确!请重新输入！");
                    $("#memeberPhone").focus();
                    return false;
                }
                var si= $("#isExist").val();
                if(si=="1"){
                    layer.msg("此手机号已存在，请重新输入！");
                    return false;
                }
                var introducer = $("#introducer").val();
                if(introducer==""||introducer==null){
                    $.ajax({
                        url: '/memberInfo/addMember.shtml',
                        type: 'post',
                        data: $("#jsForm").serialize(),
                        dataTytpe: 'json',
                        beforeSend: function () {
                            objButton.val('提交中').attr('disabled',true);
                        },
                        success: function (data) {
                            toSend = false;
                            if (data.state) {
                                layer.msg(data.message);
                                setTimeout(function () {
                                    window.parent.location.reload();
                                }, 1000)
                            } else {
                                layer.msg(data.message);
                                objButton.val('添加').removeAttr("disabled","");
                            }

                        }, error: function (data) {
                            layer.msg("抱歉，服务器刚刚打了个盹！")
                            toSend = true;
                            objButton.val('添加').removeAttr("disabled","");
                        }
                    });
                }else if(!re2.test(introducer)){
                    layer.msg("介绍人手机格式不正确!请重新输入！");
                    $("#introducer").focus();
                    return false;
                }else{
                    $.ajax({
                        url: '/memberInfo/addMember.shtml',
                        type: 'post',
                        data: $("#jsForm").serialize(),
                        dataTytpe: 'json',
                        beforeSend: function () {
                            objButton.val('提交中').attr('disabled',true);
                        },
                        success: function (data) {
                            if (data.state) {
                                layer.msg(data.message);
                                setTimeout(function () {
                                    window.parent.location.reload();
                                }, 1000)
                            } else {
                                layer.msg(data.message);
                                objButton.val('添加').removeAttr("disabled","");
                            }
                        }, error: function (data) {
                            layer.msg("抱歉，服务器刚刚打了个盹！")
                            objButton.val('添加').removeAttr("disabled","");
                        }
                    });
                }
            }
        });
        $("#memeberPhone").blur(function(){
            var re = /0?(13|14|15|18|17|19)[0-9]{9}/;
            if($(this).val()==null||$(this).val()==""){
                return false;
            }
            if(re.test($(this).val())==false){
                layer.msg("手机格式不正确，请重新输入！");
                $("#memeberPhone").focus();
            }else{
                $.ajax({
                    url:'/memberInfo/selectMemberPhone.shtml',
                    type:'post',
                    data:{phone:$(this).val()},
                    dataType:'json',
                    success:function(data){
                        if(data.state){
                            $("#isExist").val(1);
                            //$("#memeberPhone").val('');
                            layer.msg(data.message);
                        }else{
                            $("#isExist").val(2);
                        }
                    }
                })
            }
        });

        /*$("#memberName").blur(function(){
            var memberName=$(this).val();
            var rea = /^([\u4e00-\u9fa5]{1,6})$/;
            if(memberName==null||memberName==""){
                return false;
            }
//            if(!rea.test(memberName)){


//                layer.msg("会员姓名输入不合法,限制中文！");
//                $(this).val('');
//                $("#memberName").focus();
//            }
        });*/

//        $("#introducer").blur(function(){
//            var re = /0?(13|14|15|18|17)[0-9]{9}/;
//            if(re.test($(this).val())==false){
//                layer.msg("手机格式不正确，请重新输入！");
//                $(this).val('');
//                $("#introducer").focus();
//            }
    });


    /*    //取得顾问放入下拉框
        $.ajax({
            type: "post",
            url: "GetAllSchoolServlet",
            success: function (data) {
                var obj = eval('(' + data + ')');
                for (var i=0;i<obj.length;i++){
                    var scode = obj[i].scode;
                    var sname = obj[i].sname;
                    var opt = '<option value="'+scode+'"selected>'+sname+'</option>';
                    $('#selSchool').append(opt);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("ajax取学校信息出错-"+errorThrown);
            }
        });*/

    //验证会员编号不可重复
    var valid_memNum = true;
    $("#memberNumber").blur(function () {
        valid_memNum = false;
        var memberNumber = $("#memberNumber").val();
        if(memberNumber==null||memberNumber==""){
            valid_memNum = true;
        }
        $.post('/memberInfo/selectMemberNumber.shtml',{memberNumber:memberNumber},function (data) {
            if(data.state){
                valid_memNum = true;
            }else{
                layer.msg(data.message);
            }
        })
    });

    function valid(){
        var memberName=$("#memberName").val();
        if(memberName==''||memberName==null){
            layer.msg("请输入会员姓名");
            $("#memberName").focus();
            return false;
        }
        var memeberPhone=$("#memeberPhone").val();
        if(memeberPhone==''||memeberPhone==null){
            layer.msg("请输入会员电话");
            $("#memeberPhone").focus();
            return false;
        }
        var memberType=$("#memberType").val();
        if(memberType==''||memberType==null){
            layer.msg("请选择会员类型");
            return false;
        }
        var shopId=$("#shopId").val();
        if(shopId==''||shopId==null){
            layer.msg("请选择所属门店");
            $("#shopId").focus();
            return false;
        }

        var money=$("#money").val();
        var reg=/^([0-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
        if(money==''||money==null||!reg.test(money)){
            $("#money").val("");
            layer.msg("请输入余额（最多两位小数）");
            $("#money").focus();
            return false;
        }

        var re=/^\d+$/;
        var integral=$("#integral").val();
        if(integral==''||integral==null||!re.test(integral)){
            layer.msg("请输入积分(整数)");
            $("#integral").focus();
            return false;
        }
        return true;
    }

    function checkEmpName(){
        var shopId = $("#shopId").val();
        if(shopId==''){
            shopId = $("#AV").val();
        }
            $.ajax({
                url:'/memberInfo/selectEmpNameByShopId.shtml',
                type:'post',
                data:{shopId:shopId},
                dataType:'json',
                success:function (data) {
                    var str='';
                    for(var i=0;i<data.length;i++){
                        str=str+'<option value="'+data[i].id+'">'+data[i].empName+'</option>'
                    }
                    $("#subscribeEmployeeId").html(str);
                    $("#employeeId").html(str);
                }
            });
    }
</script>
</html>